
function formCheck() {
    return $('#editForm1').validate({
        rules: {
            code : {
                required: true
            },
            text: {
                required: true

            },
            description: {
                required: true
            }

        }
    });
}

function buildButton(type) {
    var btnTemplate = {
        className: "btn2"==type?"btn-default":"btn-info"
    };
    var labelObject={"btn0":"保存","btn1":"保存继续","btn2":"关闭","btn3":"保存"};
    btnTemplate.label = labelObject[type];
    btnTemplate.callback = function () {
        if ("btn2" == type) {
            return true;//关闭
        }
        if (!formCheck().form()) {
            return false;
        }
        var $form = $("#editForm1");
        if("btn0"==type||"btn1"==type){
            $.getJSON($form.prop("action"), $form.serialize(), function (data) {
                $.messager.alert0({selector: "#editAlert1", type: data.status, html: data.body});
                if (data && data.status == "SUCCESS") {
                    if ("btn0" == type) {
                        $('#tree').treegrid('reload');
                        $.bootbox.hideAll();
                    }
                }
            });
        }
        else if("btn3"==type){
            $.getJSON(ctx+"/dictionary/update", $form.serialize(), function (data) {
                $.messager.alert0({selector: "#editAlert1", type: data.status, html: data.body});
                if (data && data.status == "SUCCESS") {
                    $('#tree').treegrid('reload');
                    $.bootbox.hideAll();
                }
            });
        }
        return false;
    }
    return btnTemplate;

}

function  doEdit(id){
    var url = ctx + "/dictionary/edit_none?id="+id+"&type=edit";
    var title = "字典编辑";
    var buttons={
        btn0: buildButton("btn3"),//编辑时候的保存按钮
        btn2: buildButton("btn2")
    };
    $.messager.dialog({url: url, title: title, buttons: buttons});
}

function doAdd(id) {
    var url = ctx + "/dictionary/edit_none?id="+id+"&type=add";
    var title = "字典增加";
    var buttons = {
        btn0: buildButton("btn0"),
        btn1: buildButton("btn1"),
        btn2: buildButton("btn2")
    };
    $.messager.dialog({url: url, title: title, buttons: buttons});
}
/**
*删除
*/
function doDelete(id,state) {

    if (!id) {
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
     var childs=$("#tree").treegrid("getChildren",id);

 if(childs.length>0||!state ){
     $.messager.alert("请先删除下级数据");
     return;
 }
    var url = ctx + "/dictionary/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if(ck){
            var ary=[];
            ary.push(id);
            $.getJSON(url, {ids: ary}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.status, html: data.body});
                if (data && data.status == 'SUCCESS') {
                    $('#tree').treegrid('reload');
                }
            });
        }
    });
}
/**查询*/
function doSearch(){
    var text=$("input[name=text]").val();
    var status=$("select[name=status]").val();
    var $treeGrid=$('#tree');
    var queryParams =$treeGrid.treegrid('options').queryParams;
    queryParams.text = text;
    queryParams.status=status;
    //重新加载datagrid的数据
    $treeGrid.treegrid('reload');
}
easyloader.base="../resources/js/plugins/lib/"
easyloader.locale="zh_CN";
easyloader.theme="bootstrap"
$(function(){
    using('treegrid',function(){
        $('#tree').treegrid({
            url: ctx+"/dictionary/list",
            idField: 'id',
            treeField: 'text',
           // lines: true,
            nowrap:true,
            autoRowHeight:false,
            //rownumbers: true,
            pagination: true,
            animate: false,
            pageList: [5,10,15],
            striped:true, //隔行变色
            columns: [[
                { title: 'Id', field: 'id',hidden:true},
                { title: '标题', field: 'text' ,width:'150',align: 'center'},
                { title: '编码', field: 'code' ,width:'150',align: 'center'},

                { title: '描述', field: 'description' ,width:'150',align: 'center'},
                { title: '状态', field: 'status',width:'120',align: 'center',
                    formatter:function(value,rowData,rowIndex){
                        return  "1"==value?"正常":"锁定"

                    }},
                { title: '创建时间', field: 'createDate',width:'180',align: 'center'},

                { title: '操作', field: ' ' ,width:'230',align: 'center',
                    formatter:function(value,rowData,rowIndex){
                        return "<a href='javascript:void(0)' onclick='doEdit("+rowData.id+")'><span class='label label-xlg label-purple arrowed-in'>编辑</span></a>" +
                            "<a href='javascript:void(0)' onclick='doDelete("+rowData.id+","+rowData.id+")'><span class='label label-xlg label-warning arrowed-in'>删除</span></a>" +
                            "<a href='javascript:void(0)' onclick='doAdd("+rowData.id+")'><span class='label label-xlg label-pink arrowed-in'>添加下级</span></a>";
                    }},

                { title: 'parentId', field: 'parentId' ,hidden:true},
                { title: 'sort', field: 'sort' ,hidden:true}

            ]],
            onBeforeLoad: function(row,param){
                if (!row) {    // load top level rows
                    param.id = 0;    // set id=0, indicate to load new page rows
                }
            }

        });

    })
})

/*rowStyler:function(index,row){
    if (index%2==0){
        return 'background-color:pink;color:blue;font-weight:bold;';
    }
}
}); */